package com.hotel.front.controller.company;

import com.hotel.comm.bean.DubboReply;
import com.hotel.comm.bean.IReply;
import com.hotel.comm.bean.company.EjobCompany;
import com.hotel.comm.bean.company.EjobCompanyPosition;
import com.hotel.comm.bean.company.EjobCompanyUser;
import com.hotel.comm.service.company.ICompanyPositionService;
import com.hotel.comm.service.company.ICompanyService;
import com.hotel.comm.vo.company.EjobCompanyVo;
import com.hotel.front.bean.Constants;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class CompanyController {
    @Autowired
    private ICompanyPositionService companyPositionService;

    @Autowired
    private ICompanyService companyService;

    @SuppressWarnings({"rawtypes", "unchecked"})
    @RequestMapping(value = "/findByCityId")
    public void findByCityId(@RequestParam(value = "cityId", defaultValue = "") String cityId,
                             HttpServletRequest request, HttpServletResponse response) {
        Long _cityId = null;
        if (StringUtils.isNotBlank(cityId)) {
            _cityId = Long.parseLong(cityId);
        }
        IReply reply = companyPositionService.findByCityId(_cityId);
        if (reply.isSuccess()) {
            List<EjobCompanyPosition> positions = (List<EjobCompanyPosition>) ((DubboReply) reply).getCollection();
            for (EjobCompanyPosition position : positions) {
                System.out.println(position);
            }
        } else {
            System.out.println(((DubboReply) reply).getReplyMsg());
        }
    }

    @RequestMapping(value = "/create_company", method = RequestMethod.POST)
    @ResponseBody
    public DubboReply createCompany(EjobCompany ejobCompany, HttpServletRequest request) {

        EjobCompanyUser ejobCompanyUser = (EjobCompanyUser) request.getSession().getAttribute(Constants.LOGIN_USER);
        if (ejobCompanyUser != null) {
            ejobCompany.setEjobCompanyUser(ejobCompanyUser);
        }

        return (DubboReply) companyService.save(ejobCompany);
    }

    @RequestMapping(value = "/update_company", method = RequestMethod.POST)
    @ResponseBody
    public DubboReply updateCompany(EjobCompanyVo ejobCompanyVo) {
        return (DubboReply) companyService.update(ejobCompanyVo);
    }

    @RequestMapping(value = "/get_company_by_id")
    @ResponseBody
    public EjobCompany getCompanyById(@RequestParam(value = "id", defaultValue = "") Long id) {
        if (id == null) return null;
        IReply reply = companyService.getById(id);
        if (reply != null && reply.isSuccess()) {
            EjobCompany company = (EjobCompany) ((DubboReply) reply).getT();
            if (company != null) return company;
        }
        return null;
    }

    @RequestMapping(value = "/get_company_position_by_city_id")
    @ResponseBody
    public DubboReply getCompanyPositionById(@RequestParam(value = "cityId", defaultValue = "") Long cityId) {
        if (cityId == null) return null;
        return (DubboReply) companyPositionService.findByCityId(cityId);
    }
}
